package com.shiguiwu.springmybatis.spring.aop.principle;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

import java.lang.reflect.Method;

/**
 * @description: 前置方法通知
 * @author: stone
 * @date: Created by 2021/5/20 20:18
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.aop.principle
 */
public class BeforeMethodTests {

    public static void main(String[] args) {
        ProxyFactory proxyFactory = new ProxyFactory(new FunsService());
        proxyFactory.addAdvice(new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] objects, Object o) throws Throwable {
                String username = (String) objects[0];
                if (!"路人".equals(username)) {
                    throw new RuntimeException(String.format("%s非法访问", username));
                }


            }
        });

        Object proxy = proxyFactory.getProxy();
        FunsService funsService = (FunsService) proxy;
        funsService.getBalance("路人");
//        funsService.getBalance("张学友");
        funsService.recharge("路人", 100);
        System.out.println(funsService.getBalance("路人"));

    }
}
